//1. 导包
const {src,dest,watch} = require('gulp');
const sass = require('gulp-sass');
const cssnano = require('gulp-cssnano');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');

//2. 发布任务
//拷贝
function fnCopyIndex(){
    return src('./src/index.html')
        .pipe(dest('./dist'));
}
//css
function fnCSS(){
    return src('./src/sass/*.scss')
        .pipe(sass({outputStyle: 'expanded'}))
        // .pipe(cssnano())
        .pipe(rename({suffix : '.min'}))
        .pipe(dest('./dist/css'));
}
//js
function fnJS(){
    return src('./src/js/*.js')
        .pipe(uglify())
        .pipe(rename({suffix : '.min'}))
        .pipe(dest('./dist/js'));
}
//html
function fnHTML(){
    return src('./src/html/*.html')
        .pipe(htmlmin())
        .pipe(dest('./dist/html'));
}
//img
function fnImg(){
    return src('./src/imgs/*')
        .pipe(imagemin())
        .pipe(dest('./dist/img'));
}
//监听
function fnWatch(){
    watch('./src/index.html',fnCopyIndex);
    watch('./src/html/*.html',fnHTML);
    watch('./src/js/*.js',fnJS);
    watch('./src/sass/*.scss',fnCSS);
}
//3. 导出
exports.copyIndex = fnCopyIndex;
exports.html = fnHTML;
exports.img = fnImg;
exports.js = fnJS;
exports.sass = fnCSS;
exports.default = fnWatch;